package com.site.wisdom_construction.controller.scheduleModule;

import com.site.wisdom_construction.entity.personnelModule.Project;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.personnelModule.ProjectService;
import com.site.wisdom_construction.service.scheduleModule.ProductionRecordsService;
import com.site.wisdom_construction.statisticsResult.ProductionRecordsByHt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/12
 * @Description
 */
@Api(tags = "产值记录接口")
@RestController
@RequestMapping("ProductionRecords")
@CrossOrigin
public class ProductionRecordsController {
    @Autowired
    private ProductionRecordsService productionRecordsService;

    @Autowired
    private ProjectService projectService;

    @ApiOperation(value = "增加产值记录", notes = "测试数据:")
    @PostMapping("/insertProductionRecords")
    public JsonResult insertProductionRecords(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = productionRecordsService.insertProductionRecords(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入产值记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入产值记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除产值记录信息", notes = "测试数据:")
    @PostMapping("/deleteProductionRecords")
    public JsonResult deleteProductionRecords(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = productionRecordsService.deleteProductionRecords(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除产值记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除产值记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改产值记录", notes = "测试数据:")
    @PostMapping("/updateProductionRecords")
    public JsonResult updateProductionRecords(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = productionRecordsService.updateProductionRecords(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改产值记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改产值记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询产值记录", notes = "测试数据:")
    @PostMapping("/findProductionRecordsByPage")
    public Page findProductionRecordsByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(productionRecordsService.Total(map));
        page.setItems(productionRecordsService.findProductionRecordsByPage(map));
        return page;
    }

    @ApiOperation(value = "进度管理_产值分析(大屏)" , notes = "测试数据:")
    @PostMapping("/selectProductionRecords")
    public OneDataResult selectProductionRecords(@RequestBody Map map){
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(productionRecordsService.selectProductionRecords(map));
        return oneDataResult;
    }

    @ApiOperation(value = "进度管理_施工产值(大屏)" , notes = "测试数据:")
    @PostMapping("/sumProductionRecords")
    public OneDataResult sumProductionRecords(@RequestBody Map map){
        OneDataResult oneDataResult = new OneDataResult();
        Project project = projectService.findProjectByProjectId(map);


        ProductionRecordsByHt productionRecordsByHt = new ProductionRecordsByHt();
        productionRecordsByHt.setSumProductionMonth(productionRecordsService.SumProductionRecordsMonth(map));
        productionRecordsByHt.setSumProductionQuarter(productionRecordsService.SumProductionRecordsQuarter(map));
        productionRecordsByHt.setSumProductionAll(productionRecordsService.SumProductionRecordsAll(map));

        Double SumProjectAll = Double.valueOf(project.getConstructionCosts());
        Double SumProductionAll =
                Double.valueOf(productionRecordsService.SumProductionRecordsAll(map));
        Double d = (SumProductionAll / SumProjectAll) * 100;
        productionRecordsByHt.setSumProjectAll(SumProjectAll);
        String workerTrain = String.valueOf(d);
        // 完成率
        if ("NaN".equals(workerTrain) || "0.0".equals(workerTrain)) {
            productionRecordsByHt.setPercentageComplete("0");
        } else if ("100.0".equals(workerTrain)) {
            productionRecordsByHt.setPercentageComplete("100");
        } else {
            if (workerTrain.length() == 3) {
                productionRecordsByHt.setPercentageComplete(workerTrain.substring(0, 3));
            } else if (workerTrain.length() == 4) {
                productionRecordsByHt.setPercentageComplete(workerTrain.substring(0, 4));
            } else {
                productionRecordsByHt.setPercentageComplete(workerTrain.substring(0, 5));
            }
        }
        oneDataResult.setData(productionRecordsByHt);


        return oneDataResult;
    }
}
